package com.photo.tagger;

import com.photo.tagger.ImageMetadataDBAdapter.DatabaseHelper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class TagDataDBAdapter
{

	public static final String Tag_ID = "tag_id";
	public static final String Tag_NAME = "tag_name";
	public static final String X_COORDINATE = "x-coord";
	public static final String Y_COORDINATE = "y-coord";
	public static final String TAG_CONTACT = "tag_contact";
	public static final String TAG_TEXT = "tag_text";
	public static final String contact = "is_contact";

	private static final String DATABASE_NAME = "TagDB";
	private static final String DATABASE_TABLE = "Tags";
	private static final int DATABASE_VERSION = 1;

	private static final String DATABASE_CREATE = "create table tags(tag_id integer primary key autoincrement,"
			+ "tag_name text not null,x-coord text not null,y-coord text not null, tag_contact text,tag_text text,contact boolean);";

	private Context context;
	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;

	public void DBAdapter(Context ctx)
	{
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);
	}

	public class DatabaseHelper extends SQLiteOpenHelper
	{

		public DatabaseHelper(Context context)
		{
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
			// TODO Auto-generated constructor stub
		}

		public DatabaseHelper(Context context, String name,
				CursorFactory factory, int version)
		{
			super(context, name, factory, version);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase arg0)
		{
			// TODO Auto-generated method stub
			try
			{
				db.execSQL(DATABASE_CREATE);
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}

		@Override
		public void onUpgrade(SQLiteDatabase arg0, int oldVersion,
				int newVersion)
		{
			// TODO Auto-generated method stub
			Log.w("tag", "Upgrading database from version " + oldVersion
					+ " to " + newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS contacts");
			onCreate(db);
		}



	}
	// ---opens the database---
	public TagDataDBAdapter open() throws SQLException

	{
		db = DBHelper.getWritableDatabase();
		return this;
	}
	// ---closes the database---
	public void close()
	{
		DBHelper.close();
	}
	
	public long insertTagdata(String name,String xcoord,String ycoord,String contact,String text, boolean cont) {
		ContentValues content = new ContentValues();
		content.put(Tag_NAME, name);
		content.put(X_COORDINATE, xcoord);
		content.put(Y_COORDINATE, ycoord);
		content.put(TAG_CONTACT, contact);
		content.put(TAG_TEXT, text);
		content.put(contact,cont);
		
		
		return db.insert(DATABASE_TABLE, null, content);
	}
	public boolean deleteTagdata(String TagID) {
		return db.delete(DATABASE_TABLE, Tag_ID + "=" + TagID, null) > 0;
	}

	public Cursor getTagData(String TagID) throws SQLException {
		Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] { Tag_ID,
				Tag_NAME, X_COORDINATE , Y_COORDINATE , TAG_CONTACT , TAG_TEXT , contact }, Tag_ID + "=" + TagID, null,
				null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	
	public boolean updateTagdata(String tagid,String name,String xcoord,String ycoord,String contact,String text, boolean cont) {
		
		ContentValues content = new ContentValues();
		content.put(Tag_ID, tagid);
		content.put(Tag_NAME, name);
		content.put(X_COORDINATE, xcoord);
		content.put(Y_COORDINATE, ycoord);
		content.put(TAG_CONTACT, contact);
		content.put(TAG_TEXT, text);
		content.put(contact,cont);
		return db.update(DATABASE_TABLE, content, Tag_ID +"="+ tagid ,null)>0;
		
		
	}

}
